package com.zephyr.vchcenter.service.impl;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.log4j.Logger;
import org.codehaus.xfire.client.Client;

import sun.misc.BASE64Encoder;

import com.zephyr.vchcenter.service.IImageService;
import com.zephyr.vchcenter.util.ProperitesHelper;

public class ImageServiceImpl implements IImageService {
	private static Logger log = Logger.getLogger(IdentifyServiceImpl.class);
	
	public String getImage(int id,String xml) {
		Client client = null;
		try {
			client = new Client(new URL(ProperitesHelper.getImageServerUrl()));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Object[] argvs = null;
		argvs = new Object[] { id,xml };
		String uRet = null;
		try {
			uRet = client.invoke("zms", argvs)[0].toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return uRet;
	}

	public String getLocalImage(String imgName) {
		String string = null;
		try {
			InputStream in = new FileInputStream(imgName);
			// in.available()返回文件的字节长度
			byte[] bytes = new byte[in.available()];
			// 将文件中的内容读入到数组中
			in.read(bytes);
			string = new BASE64Encoder().encode(bytes);
			in.close();
		} catch (FileNotFoundException fe) {
			fe.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		return string;
	}
	
	public String saveImage(int id,String xml) {
		Client client = null;
		try {//ProperitesHelper.getImageServerUrl()
			client = new Client(new URL(ProperitesHelper.getImageServerUrl()));
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Object[] argvs = null;
		argvs = new Object[] { id,xml };
		String uRet = null;
		try {
			uRet = client.invoke("zms", argvs)[0].toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return uRet;
	}
	

}
